# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=qtwebkit
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=5.212.0-alpha4
pkgver=${_pkgver//-/}
pkgrel=12
pkgdesc="Webkit module for Qt5 (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/annulen/webkit/wiki"
options=(!strip)
license=(GPL3 LGPL3 FDL custom)
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ruby"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "dos2unix"
             "gperf")
depends=("${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-qt5-location"
         "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
         "${MINGW_PACKAGE_PREFIX}-qt5-sensors"
         "${MINGW_PACKAGE_PREFIX}-qt5-webchannel"
         "${MINGW_PACKAGE_PREFIX}-woff2")
#install=${_realname}-qt5-git-${CARCH}.install
source=(https://github.com/annulen/webkit/releases/download/qtwebkit-${_pkgver}/${_realname}-${_pkgver}.tar.xz
        0003-qtwebkit-mfence-mingw.patch
        0005-fix-icu-find.patch
        0006-python-output-unix-line-endings.patch
        0008-fix-using-msys-perl.patch
        0011-mingw-posix-layout-files.patch
        0014-disable-asm-win64.patch
        0015-use-proper-import-suffix-mingw.patch
        0016-separate-debug-into-bin.patch
        0202-install-private-headers-and-modules.patch
        0300-fix-generating-module-files.patch
        0501-build-gcc9.patch
        0502-clang-compat.patch
        0503-python3.patch
        0504-qwebinspector-crash-workaround.patch
        d92b11fea65364fefa700249bd3340e0cd4c5b31.patch
        6e18844035d9c5e25d770ed817b858c2423d894b.patch
        4e1f209f21c3958e5a8d3a0646dccf9e429ca7e4.patch)
sha256sums=('9ca126da9273664dd23a3ccd0c9bebceb7bb534bddd743db31caf6a5a6d4a9e6'
            'ed80d9a31cb1f9565841e3e206d76810881b7fce30210022270bd19694a9c906'
            'b9e39597d140f3fc40d07ae0f0eea0cbabd7b16d8e430d26445ae3063b8ad055'
            '02d0eb76207c4c2d36a50291976f8cc141fcff030c4b77c399402cd10a789b99'
            '9ef33dc05e5652362e10d34d75fd2cf9e7952c1c99306eb2402367401f86770b'
            'a1ef305a81b4e1b70290d4c3f70bee545f06c17736fe77abde474dba0df8c5c7'
            'c39b65235a61217bf4197285d21765a47a91d2e8c267f4b777c982d56d32b408'
            '7406d9d008e0c657b6d8e6cea20adeb18ad485fc3c749c0fc4ec98dfe1f28e36'
            'c634eedfd1645264626298400c7fbfc9c6874de08280ba841e7a3e9a71195f74'
            'bc65b6e2bca1d8b304b063a4783dbcb845f0554202c7e2035f2cdca9b165d8ea'
            '5ab5de5ee65fcd2a00412c8a2a1b2bbc4281d4ad99bac743757e6e8199a75ebe'
            'f6e85179e3581456cdfa9a00af537b4fb3caf0d66b14becc6592a15deba11175'
            '17bfe3bf5dde380b16198d9f63c7f74032c1fa7e8530047625f0f413d365a722'
            '08031fdddd5e0ce66d5ca8343d0eac80345795a9c2a30610a67ca10acbb3f5a1'
            '491826ff49965c007137e95164fde7d28bd1eb24cf929406c5c6748259d54ea6'
            'cc5a2b762426e9cda5a3ae056bba266b5f775ee75c0590913839c255d5f10db0'
            '49d4b19885806f82bff65b09e575488de176e63d91fad4545fda520aec96af82'
            '8e5f2d183c05d740d5eb1c3a191796bfb43743a462c5a0341f8171202f048af7')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"

  apply_patch_with_msg \
    0003-qtwebkit-mfence-mingw.patch \
    0005-fix-icu-find.patch \
    0006-python-output-unix-line-endings.patch \
    0008-fix-using-msys-perl.patch \
    0011-mingw-posix-layout-files.patch \
    0015-use-proper-import-suffix-mingw.patch \
    0014-disable-asm-win64.patch \
    0016-separate-debug-into-bin.patch

  # Upstream patches
  apply_patch_with_msg \
    0202-install-private-headers-and-modules.patch

  apply_patch_with_msg \
    0300-fix-generating-module-files.patch \
    0501-build-gcc9.patch \
    0502-clang-compat.patch \
    0503-python3.patch \
    0504-qwebinspector-crash-workaround.patch

  # Fix build with bison 3.7+
  apply_patch_with_msg \
    d92b11fea65364fefa700249bd3340e0cd4c5b31.patch \
    6e18844035d9c5e25d770ed817b858c2423d894b.patch \
    4e1f209f21c3958e5a8d3a0646dccf9e429ca7e4.patch

  # MSYS2 gperf cannot handle \r\n.
  find . -name "*.gperf" -exec dos2unix "{}" \;
}

build() {
  local jit_opt="-DENABLE_JIT=ON"
  if [ "${CARCH}" = "x86_64" ]; then
    local jit_opt="-DENABLE_JIT=OFF"
  fi

  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DPORT=Qt \
    -DUSE_QT_MULTIMEDIA=ON \
    -DSEPARATE_DEBUG_INFO=ON \
    ${jit_opt} \
    ../${_realname}-${_pkgver}

  cmake --build .
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  DESTDIR="${pkgdir}" cmake --install .
}
